package landian.com.module_base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.simple.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import landian.com.module_base.mvp.IView;

/**
 * Created by Administrator on 2019-03-04.
 */

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements IView {
    protected P mPresenter;
    private Unbinder bind;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getContentView(), null);
        if (enableButterKnife()) {
            bind = ButterKnife.bind(this, view);
        }
        return view;
    }
//(1)  onViewCreated在onCreateView执行完后立即执行。

//(2)  onCreateView返回的就是fragment要显示的view

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (enableEventBus()) {
            EventBus.getDefault().register(this);
        }
        mPresenter = initPresenter();
        if (mPresenter != null)
            mPresenter.attachView(this);
        initView(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (enableButterKnife()) {
            bind.unbind();
        }
        if (enableEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    protected abstract int getContentView();

    protected abstract void initView(Bundle savedInstanceState);

    protected abstract P initPresenter();

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void hideLoadingDialog() {

    }

    @Override
    public boolean enableButterKnife() {
        return false;
    }

    @Override
    public boolean enableStatusUtil() {
        return false;
    }

    @Override
    public boolean enableEventBus() {
        return false;
    }

    @Override
    public void toastTip(String msg) {

    }

    @Override
    public void toastTip(int msgId) {

    }
}
